import sys
import signal
import threading

class TimeoutException(Exception): 
    pass 

class TimeoutError(Exception): pass

def timelimit(timeout):
    def internal(function):
        def internal2(*args, **kw):
            class Calculator(threading.Thread):
                def __init__(self):
                    threading.Thread.__init__(self)
                    self.result = None
                    self.error = None
                
                def run(self):
                    try:
                        self.result = function(*args, **kw)
                    except:
                        self.error = sys.exc_info()[0]
            
            c = Calculator()
            c.start()
            c.join(timeout)
            if c.isAlive():
                raise TimeoutError
            if c.error:
                raise c.error
            return c.result
        return internal2
    return internal

def timeout(timeout_time, default):
    def timeout_function(f):
        def f2(*args,**dargs):
            def timeout_handler(signum, frame):
                raise TimeoutException()

            old_handler = signal.signal(signal.SIGALRM, timeout_handler) 
            signal.alarm(timeout_time) # triger alarm in timeout_time seconds
            try: 
                retval = f(*args,**dargs)
            except TimeoutException,e:
                raise e
            finally:
                signal.signal(signal.SIGALRM, old_handler) 
            signal.alarm(0)
            return retval
        return f2
    return timeout_function

@timeout(3, "default name")
def get_name():
    print "Please enter a name: ",
    name = sys.stdin.readline()
    return name

@timeout(1, "default city")
def get_city():
    print "Please enter a city: ",
    city = sys.stdin.readline()
    return city

if __name__ == '__main__':
    name = get_name().rstrip("\n")
    city = get_city().rstrip("\n")
    print "Got: %s, %s" % (name, city)

